<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/9
 * Time: 9:11
 */

namespace app\sports_admin\controller;
use app\exception\BannerException;
use app\sports_admin\model\Banner as BannerModel;
use think\Request;
use app\exception\BaseException;
use app\sports_admin\service\Banner as BannerService;
use app\sports_admin\vaildate\Banner as BannerVaildate;
class Banner  extends Base
{
    protected $beforeActionList = [

    ];
    //轮播图 页面
    public function index()
    {
        $data   = BannerModel::select();
        return view("index",compact("data"));
    }
    //轮播图 列表
    public function bannerList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $result = BannerModel::bannerList($page,$rows,$sort,$sortOrder);
        return json($result);
    }
    //修改排序字段值
    public function updateSort()
    {
        (new BannerVaildate())->updateSort();
        return BannerService::updateSort();
    }
    //轮播图 删除
    public function bannerDel(Request $request)
    {
        if($request->isAjax()){
            (new BannerVaildate())->go_check();
            $data = BannerModel::bannerDel();
            if(!$data){
                throw new BannerException([
                    'msg'=>'删除失败',
                    'errorCode'=>400]);
            }
            return json(['code'=>202,'msg'=>'删除成功']);
        }
    }
    //添加轮播图页面
    public function bannerAddPage()
    {
        return view('add');
    }
    //添加轮播图
    public function bannerAdd(Request $request)
    {
        if($request->isAjax()){
            (new BannerVaildate())->insert();
            $data = BannerModel::bannerAdd();
            if(!$data){
                throw new BannerException([
                    'msg'=>'添加失败',
                    'errorCode'=>400
                ]);
            }
            return json(['code'=>202,'msg'=>'添加成功']);
        }
    }
    //修改导航图图片页面
    public function image()
    {
        (new BannerVaildate())->go_check();
        $data = BannerModel::where('id',input('get.id'))->field('id,photo')->find();
        return view('image',compact('data'));
    }
    //修改导航图片
    public function updateImage()
    {
        (new BannerVaildate())->updateImage();
        return BannerService::updateImage();
    }
}